package com.honry.identityaccess.infrastructure.services;

import java.math.BigInteger;
import java.security.MessageDigest;

import com.honry.common.AssertionConcern;
import com.honry.identityaccess.domain.model.identity.EncryptionService;

public class MD5EncryptionService extends AssertionConcern implements EncryptionService {
	
	@Override
	public String encryptedValue(String plainTextValue) {
		this.assertArgumentNotEmpty(
                plainTextValue,
                "加密的文本不能为空.");

        String encryptedValue = null;

        try {

            MessageDigest messageDigest = MessageDigest.getInstance("MD5");

            messageDigest.update(plainTextValue.getBytes("UTF-8"));

            BigInteger bigInt = new BigInteger(1, messageDigest.digest());

            encryptedValue = bigInt.toString(16);

        } catch (Exception e) {
            throw new IllegalStateException(e);
        }

        return encryptedValue;
	}

}
